package com.situ.controller;

import com.situ.bean.Product;
import com.situ.bean.ProfileData;
import com.situ.bean.User;
import com.situ.bean.Useraddress;
import com.situ.service.IUserService;
import com.situ.service.IUseraddressService;
import com.situ.service.impl.ProductService;
import com.situ.util.SYS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class UserInfoController {

    @GetMapping("/api/user-info")
    public User getUserInfo(HttpSession session) {
        // 从 session 中获取用户信息
     User user = (User) session.getAttribute(SYS.UPLOAD_USER);
      return user;



    }
    @Autowired
    IUserService userService;
    @Autowired
    IUseraddressService useraddressService;
    @PostMapping("/api/update-profile")
    public ResponseEntity<?> updateProfile(@RequestBody ProfileData profileData, HttpSession session) {
        System.out.println(profileData);

            // 从请求体中获取数据
            String username = profileData.getUsername();
            String tel = profileData.getTel();
            Useraddress userAddress =(Useraddress) profileData.getUseraddress();
            userAddress.setUserid(((User)session.getAttribute(SYS.UPLOAD_USER)).getId());

            User a=new User();
            a.setUsername(username);
            a.setTel(tel);
            // 更新用户信息
            userService.updateUser(a);
            System.out.println("a跟新成功");
            // 更新用户地址信息
            System.out.println(userAddress);
            useraddressService.updateUseraddress(userAddress);
            // 构建响应数据
            System.out.println("保存成功");
            return ResponseEntity.ok(a);



    }





        @Autowired
        private ProductService productService;

    @RequestMapping("/api/search")
        public List<Product> searchProducts(@RequestParam String query) {

            return productService.searchProducts(query);
        }








}














